//go:build swagger
// +build swagger

package server

import (
	"github.com/starmotioncf/soutchatapi_docs"

	"github.com/gin-gonic/gin"
	swaggerfiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func init() {
	registerRouters = append(registerRouters, SwagRegister)
}

// 访问swagger文档密码
const swaggerPwd = "842410"

func SwagRegister(r gin.IRouter) {
	// swagger doc
	api_docs.SwaggerInfo.BasePath = ""
	r.GET("/api/swagger/*any", func() gin.HandlerFunc {
		ginHandleFn := ginSwagger.WrapHandler(
			swaggerfiles.Handler,
			ginSwagger.DefaultModelsExpandDepth(-1),
		)
		return func(ctx *gin.Context) {
			if ctx.Request.URL.Path == "/api/swagger/index.html" && ctx.Query("key") != swaggerPwd {
				ctx.AbortWithStatus(401)
				return
			}
			ginHandleFn(ctx)
		}
	}())
}
